<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
//for stock

ini_set('max_execution_time', 900);
set_time_limit(900);

ini_set("display_errors",0);
error_reporting(E_ALL);
//error_reporting(0);
echo '<pre>';

require_once('simplesql.class.php5');
require_once('function-getfile.php');
function getfile($filename){
	$handle = fopen('product/' .$filename, "r");
	$myarticles = fread($handle, filesize('product/' .$filename));
	fclose($handle);
	
	$objInfo = unserialize($myarticles);
	$myarrInfo = toArray($objInfo);	
	$arrInfo = array();
	if(count($myarrInfo)){
		$arrInfo = $myarrInfo;
	}
	return $arrInfo;
}
	$ini = parse_ini_file("web.ini");
	while(list($key,$value) = each($ini)){
		if($key == 'hostName'){
			$hostName = $value;
		}
		else if($key == 'userName'){
			$userName = $value;
		}
		else if($key == 'password'){
			$password = $value;
		}
		else if($key == 'dbName'){
			$dbName = $value;
		}
	}

	$db = new simplesql($hostName,$userName,$password,$dbName);
	$db->query("SET NAMES utf8");
	
	$filename = '';
	foreach(new DirectoryIterator('product/') as $file)
	{
		if ($file->isFile()) {
			//echo $file, "\n";
			$filename = $file;
			break;
		}        
	}
	
	if(empty($filename)){
		getdata();
		exit;
	}
	
	$myarticles = '';
	$myarrInfo = array();
	$objInfo = array();
	/*
	if(file_exists('product/' . $filename)){
	
		//$myarticles = file_get_contents('product/' .$filename);
		
		$handle = fopen('product/' .$filename, "r");
		$myarticles = fread($handle, filesize('product/' .$filename));
		fclose($handle);
		
		$objInfo = unserialize($myarticles);
		$myarrInfo = toArray($objInfo);	
		$arrInfo = array();
		if(count($myarrInfo)){
			$arrInfo = $myarrInfo;
		}else{
			getdata();
			exit;
		}
	}else{
	
		getdata();
		exit;
	}
	*/
	
	$arrInfo = getfile("8.txt");
	$error = false;
	$result = true;
	/**
	* insert, update Manufacturer
	**/
	//get data group by SupplierName
	echo "<br />-----------<br />";
	echo date("Y-m-d H:i:s");
	$new_list = group_assoc($arrInfo, 'Supplier');
	$arr_manufacturer = array();
	foreach($new_list as $key => $value){
		$supplier = $key;
		$manufacturer_name = $value[0]['SupplierName'];
		$arr_manufacturer[$key]=$manufacturer_name;
	}
	//insertManufacturers($db, $arr_manufacturer);
	
	/**
	* insert, update category
	**/
	
	$new_list = group_assoc($arrInfo, 'ItemGroup');
	$arr_category = array();
	foreach($new_list as $key => $value){
		$itemgroup = $key;
		$category_name = $value[0]['ItemGroupName'];
		$arr_category[$key]=$category_name;
	}
	
	//insertCategories($db, $arr_category);
	
	/**
	* insert, update attribute
	**/

	$group_products = group_assoc($arrInfo, 'KatalogNumber');
	//insertAttribute($db, $group_products);
	
	/**
	* insert, update products
	**/
	
	//insertProducts($db, $group_products);
	
	/**
	* insert, update option to products
	**/
	
	foreach($arrInfo as $k=>$v){
		if($v['KatalogNumber'] != '983'){
			unset($arrInfo[$k]);
		}
	}
	
	//var_dump($arrInfo);
	insertOptiontoProduct($db, $arrInfo);
	
	/**
	* update total stock
	**/
	
	//updateTotalStock($db);
	
	/**
	* remove file which has just been updated.
	**/
	
	//unlink('product/'.$filename);
	echo "<br />";
	echo date("Y-m-d H:i:s");
	
	echo '</pre>';


?>
</body>
</html>